﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ToolCalendar
{
    public class Base : Form
    {
        public string[] time = { "00:00", "00:30", "01:00", "01:30", "02:00", "02:30", "03:00", "03:30", "04:00", "04:30", "05:00", "05:30", "06:00", "06:30", "07:00", "07:30", "08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30" };
        public string[] time1 = { "00:00", "00:30", "01:00", "01:30", "02:00", "02:30", "03:00", "03:30", "04:00", "04:30", "05:00", "05:30", "06:00", "06:30", "07:00", "07:30", "08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30" };
        public string location = "沈阳市, 中国辽宁省沈阳市";

        public Base()
        {
            this.Icon = global::ToolCalendar.Properties.Resources.main;
        }

        #region 设置默认位置

        /// <summary>
        /// 默认位置
        /// </summary>
        public void SetDefaultLocation()
        {
            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);

            this.Left = (Convert.ToInt32(rect.Width * 0.8) - this.Width) / 2;
            this.Top = (Convert.ToInt32(rect.Height * 0.8) - this.Height) / 2;
        }
        #endregion


        /// <summary>
        /// 设置时间名称
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public string GetTimeName(Dictionary<string, bool> dic, string start, string end)
        {
            var result = "";
            if (dic["明日计划"] && start == "17:30" && end == "17:30")
            {
                dic["明日计划"] = false;
                result = "明日计划";
            }
            else if (dic["上午"] && Convert.ToDateTime("2018-01-01 " + start) >= Convert.ToDateTime("2018-01-01") && Convert.ToDateTime("2018-01-01 " + start) < Convert.ToDateTime("2018-01-01 12:00"))
            {
                dic["上午"] = false;
                result = "上午";
            }
            else if (dic["下午"] && Convert.ToDateTime("2018-01-01 " + start) >= Convert.ToDateTime("2018-01-01 12:00") && Convert.ToDateTime("2018-01-01 " + start) < Convert.ToDateTime("2018-01-01 18:00"))
            {
                dic["下午"] = false;
                result = "下午";
            }
            else if (dic["晚上"] && Convert.ToDateTime("2018-01-01 " + start) >= Convert.ToDateTime("2018-01-01 18:00"))
            {
                dic["晚上"] = false;
                result = "晚上";
            }
            return result;
        }


        /// <summary>
        /// 获取事件
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public string GetEevents(List<Model.CalenderModel> list, out string timeStr)
        {
            timeStr = DateTime.Now.ToString("HH:mm");
            var dic = new Dictionary<string, bool>()
            {
                {"上午",true },
                {"下午",true },
                {"明日计划",true },
                {"晚上",true }
            };

            //列出事件。
            var str = new StringBuilder();

            foreach (var item in list)
            {
                var nr = item.Title;

                timeStr = item.End.ToString("HH:mm");

                var timeName = GetTimeName(dic, item.Start.ToString("HH:mm"), item.End.ToString("HH:mm"));
                if (!string.IsNullOrEmpty(timeName))
                {
                    str.Append(timeName + "\r\n");
                }
                var haoshi = (item.End - item.Start).TotalHours;
                var hs = "";
                if (haoshi >= 1)
                {
                    hs = haoshi + "小时";
                }
                else
                {
                    hs = (haoshi * 60) + "分钟";
                }
                if (item.Start == item.End)//明日计划
                {
                    nr = item.Title;
                }
                else
                {
                    nr = string.Format("[{0}-{1} {2}]:{3}", item.Start.ToString("HH:mm"), item.End.ToString("HH:mm"), hs, item.Title);
                }
                str.Append(nr + "\r\n");
            }
            return str.ToString();
        }

    }
}
